<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @cancel="handleCancel"
    :body-style="{
      padding: '10px'
    }"
    cancelText="关闭"
  >
    <template #footer>
      <a-button type="danger" @click="handleCancel">取消</a-button>
      <a-button type="primary" v-if="currentStep !== 1" @click="prevStep">上一步</a-button>
      <a-button type="primary" v-if="currentStep !== totalSteps" @click="nextStep">下一步</a-button>
      <a-button type="primary" v-if="currentStep === totalSteps" @click="handleOk">生成工单</a-button>
    </template>

    <div>
      <GenerateProductOrderSelectStep
        v-show="currentStep === 1"
        ref="selectStep"
        @check="handleSelectStep"
      ></GenerateProductOrderSelectStep>
      <GenerateProductOrderEntry v-show="currentStep === 2" ref="entry" @ok="validOk"></GenerateProductOrderEntry>
    </div>
  </j-modal>
</template>

<script>
import { postAction } from '@api/manage'
import GenerateProductOrderSelectStep from '@views/production/modules/generateProductOrder/GenerateProductOrderSelectStep.vue'
import GenerateProductOrderEntry from '@views/production/modules/generateProductOrder/GenerateProductOrderEntry.vue'

export default {
  name: 'GenerateProductOrderPreview',
  components: {
    GenerateProductOrderEntry,
    GenerateProductOrderSelectStep
  },
  props: {
    sourceId: {
      type: String,
      default: ''
    },
    sourceSerial: {
      type: String,
      default: ''
    }
  },
  watch: {},
  computed: {
    title() {
      switch (this.currentStep) {
        case 1:
          return '选择工序'
        case 2:
          return '输入数量'
        default:
          return ''
      }
    }
  },
  data() {
    return {
      currentStep: 1,
      totalSteps: 2,
      width: 1200,
      visible: false,
      confirmLoading: false,
      steps: [],
      bomIdObjs: [],
      url: {
        list: '/production/hfRouting/stepSelectByProductOrder'
      },
      dataSource: [],
      missionDetailList: [],
      selectKeys: ''
    }
  },
  methods: {
    loadData(missionDetailList) {
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }

      this.missionDetailList = missionDetailList
      this.confirmLoading = true

      postAction(this.url.list, missionDetailList)
        .then(res => {
          if (res.success) {
            this.dataSource = res.result.records || res.result
            this.steps = []
            for (let key in this.dataSource) {
              let temp = {
                key: key,
                title: key,
                children: this.dataSource[key].map(item => {
                  return {
                    title: `${item.materialCode} - ${item.materialName}`,
                    key: item.materialCode
                  }
                })
              }
              this.steps.push(temp)
            }
            this.$refs.selectStep.treeData = [...this.steps]
            this.$refs.selectStep.expandedKeys = [...this.steps].map(item => item.key)
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {
          this.confirmLoading = false
        })
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.currentStep = 1
    },
    handleCancel() {
      this.close()
    },
    validOk(isValid) {
      return isValid
    },
    handleOk() {
      // const isValid =  this.$refs.entry.handleOk(this.sourceId,this.sourceSerial)
      const isValid = this.$refs.entry.handleValid()
      if (isValid) {
        this.$refs.entry.handleOk(this.sourceId, this.sourceSerial)
        this.close()
      }
    },
    prevStep() {
      if (this.currentStep <= this.totalSteps && this.currentStep > 1) {
        this.currentStep--
      }
    },
    nextStep() {
      if (this.currentStep < this.totalSteps) {
        this.currentStep++
      }
      if (this.currentStep === 2) {
        this.$refs.entry.loadData1([...this.bomIdObjs])
        this.$refs.entry.initDictData()
      }
    },
    handleSelectStep(selectKeys) {
      // this.selectKeys = selectKeys
      let data = Object.values(this.dataSource).flat()
      let temp = []
      for (let s of selectKeys) {
        const target = data.find(item => item.materialCode === s)
        temp.push({
          ...target
        })
      }
      this.bomIdObjs = temp
    }
  }
}
</script>

<style scoped></style>
